import comfy
from utils.image import download_image, get_image_ext
from datetime import datetime

def process_image(file_path, subfolder, index=0):
  """
  处理comfy图片
  """
  image_ext = get_image_ext(file_path)
  comfy_client = comfy.get_comfy_client()
  current_time = datetime.now()
  formatted_time = current_time.strftime('%Y%m%d-%H%M%S')
  filename = f"{formatted_time}-{index}{image_ext}"
  root = 'faces/'

  # 下载图片
  download_image(file_path, root, filename=filename)
  
  # Open the file in binary mode
  with open(root + filename, 'rb') as file:
    files = {'image': (filename, file, 'image/jpeg')}
    # Make the POST request
    # 上传图片到comfy ui
    res = comfy_client.upload_image(imagefile=files, subfolder=subfolder, type='input', overwrite=False)
    print(res)
  
  return filename